<template>
	<view class="overall">
		<view class="empty" v-if="goods.length==0">
			<view class="empty-text">空空如也的购物</view>
			<view class="empty-button" @click="goshopping">去选购</view>
		</view>
		<view v-else>
			<view class="goods-detail" v-for="(item,index) in goods" :key="item.cid">
				<view class="detail-left">
					<view class="goods-left">
						<checkbox-group @change="selected(item)">
							<label>
								<checkbox class="selected" color="#555555" :checked="checked" /><text></text>
							</label>
						</checkbox-group>
						<image :src="item.image" @error="errorimg(index)"
							style="width:208rpx;height: 154rpx ; border-radius: 30rpx;">
						</image>
					</view>
					<view class="size">
						<text style="font-size: 40rpx;">{{item.title}}</text>
						<text class="goods-price" style="font-size:26rpx;">￥{{item.price}}</text>
					</view>
				</view>
				<view class="detail-right">
					<text class="subtract" @click="reduce(item)">-</text>
					<text class="num">{{item.num}}</text>
					<text @click="add(item)" class="add">+</text>
					<button class=" button-del" @click="delGoods(item.cid,index)">移除购物车</button>
				</view>

			</view>
		</view>
		<view class="end">
			<view class="end-left">
				<checkbox-group @change="selectgoods()">
					<label>
						<checkbox :checked="allchecked" />全选
					</label>
				</checkbox-group>
				<view>
					总计：<text style="color: #f00;font-weight: bold;">￥ {{totalPrice}}</text>
				</view>
			</view>
			<view class="end-right" @click="toPayment">
				结算({{totalNum}})
			</view>


		</view>
	</view>
</template>

<script>
	import {
		cartList,
		delCartGoods,
		updateCartGoods
	} from '@/api/system/cart.js'
	import {
		setOrder
	} from '@/api/system/order.js'
	export default {
		data() {
			return {
				allchecked: false,
				checked: false,
				goods: [],
				isOne: true
			}
		},
		methods: {
			goshopping() {
				uni.switchTab({
					url: '/pages/index'
				})
			},
			selected(item) {
				item.flag = !item.flag
				if (!item.flag) {
					this.allchecked = false

				} else {
					const a = this.goods.forEach((item) => {
						return item.flag === true
					})
					if (a) {
						this.allchecked = true
					} else {
						this.allchecked = false
					}
				}
			},
			selectgoods() {
				this.allchecked = !this.allchecked
				if (this.allchecked) {
					this.goods.map(item => {
						this.checked = true
						item.flag = true
					})
				} else {
					this.checked = false
					this.goods.map(item => {
						item.flag = false
					})
				}
			},
			reduce(item) {
				let num = item.num
				if (num > 1) {
					num -= 1
					updateCartGoods(item.cid, '-1')
				} else if (num = 1) {
					uni.showToast({
						title: "不能减少了哟~",
						icon: 'none',
						duration: 500
					})
				}


				item.num = num
			},
			add(item) {
				let num = item.num
				item.num = num + 1
				updateCartGoods(item.cid, '+1')
			},
			islogin() {
				if (!uni.getStorageSync('userInfo')) {
					this.$modal.msg('请先登录')
				}
			},
			getCartList() {
				if (uni.getStorageSync('userInfo')) {
					cartList().then(res => {
						if (res.code == 200) {
							res.data.forEach(res => {
								res.flag = false
								res.image ? '' : res.image = '/static/images/banner/banner01.jpg'
							})
							this.goods = res.data
							setTimeout(() => {
								this.$modal.closeLoading()
							}, 100)
						} else {
							this.$modal.closeLoading()
							this.$modal.msgError(res.msg)
						}
					})
				}
			},
			errorimg(id) {
				return this.data[id].image = '/static/images/banner/banner01.jpg'
			},
			delGoods(cid, index) {
				this.$modal.confirm('是否删除商品').then(res => {
					if (res == true) {
						delCartGoods(cid).then(res => {
							this.goods.splice(index, 1)
							if (res == 200) {
								uni.showToast({
									title: content,
									icon: 'success',
									duration: 500
								})
							}
						})
					}
				})
			},
			toPayment() {
				if (this.totalNum == 0) return this.$modal.msg('请选择商品')
				let cids = ''
				this.goods.forEach(item => {
					if (item.flag == true) {
						cids = cids + item.cid + ','
					}
				})
				const data = {
					cids: cids.substring(0, cids.length - 1)
				}
				setOrder(data).then(res => {
					if (res.code == 200) {
						this.$tab.navigateTo('/pages/car/payment?data=' + JSON.stringify(res.data))
					} else {
						this.$modal.errorMsg(res.msg)
					}
				})
			}
		},
		onShow() {
			this.islogin()
			this.getCartList()
		},
		onLoad() {
			this.$modal.loading('正在加载数据')
		},
		computed: {
			totalNum() {
				let totalNum = 0;
				this.goods.map(item => {
					item.flag ? totalNum += item.num : totalNum += 0
				})
				return totalNum
			},

			totalPrice() {
				let totalPrice = 0;
				this.goods.map(item => {
					item.flag ? totalPrice += item.num * item.price : totalPrice += 0
				})
				return totalPrice
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #65A8F2;
	}

	.goods {
		line-height: 80rpx;
		background-color: #FFFFFF;

		&-detail {
			display: flex;
			margin: 0 auto 26rpx;
			height: 236rpx;
			width: 720rpx;
			padding: 30rpx 15rpx 30rpx 30rpx;
			background-color: #fff;
			justify-content: space-between;
			border-bottom: 5rpx solid #F1F1F1;
			align-items: center;

			.detail-left {
				display: flex;

				.goods-left {
					display: flex;
					align-items: center;
				}
			}


			.size {
				display: flex;
				justify-content: space-around;
				flex-direction: column;
				margin-left: 30rpx;

				.goods-price {
					color: rgba(16, 16, 16, 1);
					font-size: 36rpx;

				}
			}

			.detail-right {
				text {
					width: 50rpx;
					line-height: 50rpx;
					text-align: center;
					display: inline-block;
					background-color: #F7F7F7;
					margin-right: 10rpx;
				}

				.add {
					color: #FA4305;
					border-radius: 10rpx 30rpx 30rpx 10rpx;
					margin-right: 20rpx;
				}

				.subtract {
					border-radius: 30rpx 10rpx 10rpx 30rpx;
				}
			}

			.button-del {
				margin: 0;
				padding: 0;
				margin-top: 30rpx;
				width: 180rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 20rpx;
				background-color: rgba(244, 68, 68, 0.73);
				font-size: 28rpx;
				text-align: center;
				border: 1px solid rgba(187, 187, 187, 1);
			}
		}
	}

	.empty {

		position: relative;
		top: 220rpx;
		text-align: center;
		display: flex;
		align-items: center;
		flex-direction: column;

		&-text {
			color: #808080;
			margin-bottom: 50rpx;
		}

		&-button {
			width: 300rpx;
			height: 90rpx;
			color: orange;
			border: 1rpx solid orange;
			text-align: center;
			line-height: 90rpx;
			border-radius: 48rpx;
		}
	}

	.end {
		width: 100%;
		height: 90rpx;
		background-color: #fff;
		position: fixed;
		bottom: 100rpx;
		left: 0;
		display: flex;
		align-items: center;
		margin-bottom: -100rpx;

		&-left {
			width: 70%;
			display: flex;
			justify-content: space-between;
			padding: 0 30rpx;
			font-size: 35rpx;
			align-items: center;

			.end-flex {
				display: flex;
				align-items: center;
			}
		}

		&-right {
			width: 30%;
			line-height: 90rpx;
			background-color: #4963F4;
			text-align: center;
			color: #fff;
			font-size: 35rpx;
			align-items: center;
		}
	}

	.selected {
		margin-right: 6rpx;
	}


	//组件样式 


	checkbox[checked]::before {
		content: '';
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		background-color: rgba(101, 168, 242, 1);

	}

	checkbox::before {
		content: '';
	}

	checkbox .wx-checkbox-input {
		border-radius: 50%;
	}

	checkbox[checked] .wx-checkbox-input {
		border-radius: 50%;
		background-color: white !important;
		border: 1px solid #ccc !important;

	}
</style>
